package com.example.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: demo
 * @description:
 * @author: Mr.Wang
 * @create: 2020-09-30 15:54
 **/
public class ReadTxtUtil {

    public static String txt2String(File file) {
        StringBuilder result = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                result.append(System.lineSeparator() + s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    public static void main(String[] args) throws Exception {
//        File file = new File("D:/course.txt");
        File file = new File("D:/exchangeItem2.txt");
        String s = txt2String(file);
        String[] posts = s.split("]----------");
        List<String> list = new ArrayList<>();
        String request = "";
        for (String post : posts) {
            String[] split = post.split("TID:");
            String end = split[1];
            String[] split1 = end.split("]");
            String tid = split1[0];
            list.add(tid);
        }
        System.out.println(list.size());

        List<String> result = new ArrayList<>();
        for (String s1 : list) {
            int a = 0;
            for (String s2 : list) {
                if (s1.equals(s2)) {
                    a++;
                }
            }
            if (a == 1)
                result.add(s1);
        }
        System.out.println(result.size());
        for (String s1 : result) {
            request = request + "\r\n" + s1;
        }

        FileOutputStream fileOutputStream = null;
        File file1 = new File("D:/exchangeItem3.txt");
        if (!file1.exists()) {
            file1.createNewFile();
        }
        fileOutputStream = new FileOutputStream(file1);
        fileOutputStream.write(request.getBytes("gbk"));
        fileOutputStream.flush();
        fileOutputStream.close();

    }
}
